package com.ruoyi.web.controller.fantasy;

import java.util.Date;
import java.util.List;

import com.huanzhidadi.fantasy.bean.domain.FantasyCity;
import com.huanzhidadi.fantasy.bean.domain.FantasyTitle;
import com.ruoyi.fantasy.domain.FantasyItemType;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.fantasy.service.IFantasyCitiesService;
import com.ruoyi.fantasy.service.IFantasyTitleService;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.web.controller.system.cloud.CloudStorageService;
import com.ruoyi.web.controller.system.cloud.OSSFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fantasy.service.IFantasyItemtypeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * 道具类型 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-25
 */
@Slf4j
@Controller
@RequestMapping("/fantasy/fantasyItemtype")
public class FantasyItemtypeController extends BaseController
{
    private String prefix = "fantasy/fantasyItemtype";
	
	@Autowired
	private IFantasyItemtypeService fantasyItemtypeService;
	@Autowired
	private IFantasyCitiesService fantasyCitiesService;

	@Autowired
	private IFantasyTitleService fantasyTitleService;

	@Autowired
	private ISysOssService sysOssService;

	@RequiresPermissions("fantasy:fantasyItemtype:view")
	@GetMapping()
	public String fantasyItemtype(ModelMap mmap)
	{
		ModelAndView mv = new ModelAndView();
		List<FantasyCity> cities = fantasyCitiesService.selectFantasyCitiesList(null);
		mmap.addAttribute("cities", cities);
	    return prefix + "/fantasyItemtype";
	}
	
	/**
	 * 查询道具类型列表
	 */
	@RequiresPermissions("fantasy:fantasyItemtype:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(FantasyItemType fantasyItemtype)
	{
		startPage();
        List<FantasyItemType> list = fantasyItemtypeService.selectFantasyItemtypeList(fantasyItemtype);
		fantasyItemtypeService.transforData(list);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出道具类型列表
	 */
	@RequiresPermissions("fantasy:fantasyItemtype:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyItemType fantasyItemtype)
    {
    	List<FantasyItemType> list = fantasyItemtypeService.selectFantasyItemtypeList(fantasyItemtype);
        ExcelUtil<FantasyItemType> util = new ExcelUtil<FantasyItemType>(FantasyItemType.class);
        return util.exportExcel(list, "fantasyItemtype");
    }
	
	/**
	 * 新增道具类型
	 */
	@GetMapping("/add")
	public String add(ModelMap mmap)
	{
		ModelAndView mv = new ModelAndView();
		List<FantasyCity> cities = fantasyCitiesService.selectFantasyCitiesList(null);
		mmap.addAttribute("cities", cities);
		FantasyTitle fantasyTitle = new FantasyTitle();
		fantasyTitle.setTypeId("7937233255133208576");
		List<FantasyTitle> fantasyTitleList = fantasyTitleService.selectFantasyTitleList(fantasyTitle);
		mmap.addAttribute("fantasyTitleList", fantasyTitleList);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存道具类型
	 */
	@RequiresPermissions("fantasy:fantasyItemtype:add")
	@Log(title = "道具类型", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(FantasyItemType fantasyItemtype, @RequestParam("item") MultipartFile file)
	{
		try {
			if (!file.isEmpty()) {
				// 上传文件
				byte[] fileBytes = file.getBytes();
				String item = FileUploadUtils.upload(Global.getItemPath(), file);
				String relativePath = item.substring(item.indexOf("/", 1) + 1);
				fantasyItemtype.setImgsrc(relativePath);
				String suffix = relativePath.substring(relativePath.lastIndexOf("."));
				CloudStorageService storage = OSSFactory.build();
				String url = storage.upload(fileBytes, relativePath);
				// 保存文件信息
				SysOss ossEntity = new SysOss();
				ossEntity.setUrl(url);
				ossEntity.setFileSuffix(suffix);
				ossEntity.setCreateBy(ShiroUtils.getLoginName());
				ossEntity.setFileName(relativePath);
				ossEntity.setCreateTime(new Date());
				ossEntity.setService(storage.getService());
				sysOssService.save(ossEntity);
			}
			// 取身份信息
			SysUser user = ShiroUtils.getSysUser();
			fantasyItemtype.setCreateBy(user.getUserId().toString());
			fantasyItemtype.setCreateTime(new Date());
			fantasyItemtype.setUpdateBy(user.getUserId().toString());
			fantasyItemtype.setUpdateTime(new Date());
			return toAjax(fantasyItemtypeService.insertFantasyItemtype(fantasyItemtype));
		} catch (Exception e) {
			log.error("保存道具类型失败！", e);
			return error(e.getMessage());
		}
	}

	/**
	 * 修改道具类型
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		List<FantasyCity> cities = fantasyCitiesService.selectFantasyCitiesList(null);
		mmap.addAttribute("cities", cities);
		FantasyItemType fantasyItemtype = fantasyItemtypeService.selectFantasyItemtypeById(id);
		mmap.put("fantasyItemtype", fantasyItemtype);
		FantasyTitle fantasyTitle = new FantasyTitle();
		fantasyTitle.setTypeId("7937233255133208576");
		List<FantasyTitle> fantasyTitleList = fantasyTitleService.selectFantasyTitleList(fantasyTitle);
		mmap.addAttribute("fantasyTitleList", fantasyTitleList);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存道具类型
	 */
	@RequiresPermissions("fantasy:fantasyItemtype:edit")
	@Log(title = "道具类型", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(FantasyItemType fantasyItemtype, @RequestParam("item") MultipartFile file)
	{
		try {
			if (!file.isEmpty()) {
				// 上传文件
				byte[] fileBytes = file.getBytes();
				String item = FileUploadUtils.upload(Global.getItemPath(), file);
				String relativePath = item.substring(item.indexOf("/", 1) + 1);
				fantasyItemtype.setImgsrc(relativePath);
				String suffix = relativePath.substring(relativePath.lastIndexOf("."));
				CloudStorageService storage = OSSFactory.build();
				String url = storage.upload(fileBytes, relativePath);
				// 保存文件信息
				SysOss ossEntity = new SysOss();
				ossEntity.setUrl(url);
				ossEntity.setFileSuffix(suffix);
				ossEntity.setCreateBy(ShiroUtils.getLoginName());
				ossEntity.setFileName(relativePath);
				ossEntity.setCreateTime(new Date());
				ossEntity.setService(storage.getService());
				sysOssService.save(ossEntity);
			}
			// 取身份信息
			SysUser user = ShiroUtils.getSysUser();
			fantasyItemtype.setUpdateBy(user.getUserId().toString());
			fantasyItemtype.setUpdateTime(new Date());
			return toAjax(fantasyItemtypeService.updateFantasyItemtype(fantasyItemtype));
		} catch (Exception e) {
			log.error("保存道具类型失败！", e);
			return error(e.getMessage());
		}
	}
	
	/**
	 * 删除道具类型
	 */
	@RequiresPermissions("fantasy:fantasyItemtype:remove")
	@Log(title = "道具类型", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(fantasyItemtypeService.deleteFantasyItemtypeByIds(ids));
	}


	/**
	 * 禁用城市
	 */
	@RequiresPermissions("fantasy:fantasyItemtype:disable")
	@Log(title = "道具类型", businessType = BusinessType.DISABLE)
	@PostMapping( "/disable")
	@ResponseBody
	public AjaxResult disable(String ids)
	{
		return toAjax(fantasyItemtypeService.disableFantasyItemtypeByIds(ids));
	}

	/**
	 * 还原城市
	 */
	@RequiresPermissions("fantasy:fantasyItemtype:disable")
	@Log(title = "道具类型", businessType = BusinessType.RESTORE)
	@PostMapping( "/restore")
	@ResponseBody
	public AjaxResult restore(String ids)
	{
		return toAjax(fantasyItemtypeService.restoreFantasyItemtypeByIds(ids));
	}
}
